package com.example.buildtable.controller;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaJoinQueryWrapper;
import com.example.buildtable.pojo.Member;
import com.example.common.util.R;
import com.example.buildtable.pojo.ComplaintMessage;
import com.example.buildtable.service.ComplaintMessageService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@Tag(name = "投诉留言")
@RequestMapping(value = "/complaintMessage/")
public class ComplaintMessageController {
    @Autowired
    private ComplaintMessageService complaintMessageService;
    
    @Operation(summary = "查询所有投诉留言数据")
    @GetMapping(value = "/getAllComplaintMessage")
    public R getAllComplaintMessage() {
        LambdaJoinQueryWrapper<ComplaintMessage> wrapper = new LambdaJoinQueryWrapper<>(ComplaintMessage.class);
        wrapper.innerJoin(Member.class, Member::getMemberId, ComplaintMessage::getMemberId);
        List<ComplaintMessage> complaintMessageList = complaintMessageService.list(wrapper);
        return R.ok("查询所有投诉留言数据成功").data("complaintMessageList", complaintMessageList);
    }

    @Operation(summary = "增加投诉留言")
    @PostMapping(value = "/addComplaintMessage")
    public R addComplaintMessage(ComplaintMessage complaintMessage) {
        boolean flag = complaintMessageService.save(complaintMessage);
        if (flag) {
            return R.ok("投诉留言成功");
        } else {
            return R.ok("投诉留言失败");
        }
    }

    @Operation(summary = "删除投诉留言")
    @GetMapping(value = "/deleteComplaintMessage/{complaintMessageId}")
    public R deleteComplaintMessage(@PathVariable("complaintMessageId") Integer complaintMessageId) {
        boolean flag = complaintMessageService.removeById(complaintMessageId);
        if (flag) {
            return R.ok("删除投诉留言成功");
        } else {
            return R.ok("删除投诉留言失败");
        }
    }

    @Operation(summary = "按照投诉留言查看")
    @GetMapping(value = "/getComplaintMessageById/{complaintMessageId}")
    public R getComplaintMessageById(@PathVariable("complaintMessageId") Integer complaintMessageId) {
        LambdaJoinQueryWrapper<ComplaintMessage> wrapper = new LambdaJoinQueryWrapper<>(ComplaintMessage.class);
        wrapper.innerJoin(Member.class, Member::getMemberId, ComplaintMessage::getMemberId);
        wrapper.eq(ComplaintMessage::getMemberId, complaintMessageId);
        ComplaintMessage complaintMessage = complaintMessageService.getOne(wrapper);
        if (complaintMessage != null) {
            return R.ok("按照投诉留言查看成功").data("complaintMessage", complaintMessage);
        } else {
            return R.error("按照投诉留言查看失败");
        }
    }

    @Operation(summary = "修改投诉留言")
    @PostMapping(value = "/updateComplaintMessage")
    public R updateComplaintMessage(ComplaintMessage complaintMessage) {
        boolean flag = complaintMessageService.updateById(complaintMessage);
        if (flag) {
            return R.ok("修改投诉留言成功");
        } else {
            return R.ok("修改投诉留言失败");
        }
    }
}